package cn.edu.nyist.integration.controller;

import cn.edu.nyist.integration.common.ServerResponse;
import cn.edu.nyist.integration.entity.Notice;
import cn.edu.nyist.integration.entity.User;
import cn.edu.nyist.integration.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @RequestMapping("/page")
    public String toNoticePage() {
        return "page/notice-add";
    }


    @RequestMapping(value = "/issue", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse issueNotice(String noticeTitle, String noticeContent, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return ServerResponse.createByErrorMessage("登录失效");
        }
        String username = user.getUsername();
        Notice notice = new Notice();
        notice.setNoticeTitle(noticeTitle);
        notice.setNoticeContent(noticeContent);
        notice.setCreateTime(new Date());
        notice.setCreateBy(username);
        notice.setUpdateBy(username);


        return noticeService.issueNotice(notice);
    }

    @RequestMapping("/list")
    public String listNotice(Model model) {
        List<Notice> noticeList = noticeService.queryAll();
        model.addAttribute("noticeList", noticeList);
        return "page/notice-list";
    }

    @RequestMapping("/edit")
    public String showNotice(Long noticeId, Model model) {
        if (noticeId == null) {
            return "redirect:/notice/list";
        }
        Notice notice = noticeService.selectOne(noticeId);

        model.addAttribute("notice", notice);
        return "page/notice-edit";

    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String updateNotice(Notice notice, Model model, HttpSession session) {

        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/";
        }
        String username = user.getUsername();
        notice.setUpdateBy(username);
        notice.setUpdateTime(new Date());

        ServerResponse response = noticeService.updateNotice(notice);
        if (!response.isSuccess()) {
            model.addAttribute("errorMsg", response.getMsg());
            return "page/notice-edit";
        }
        return "redirect:/notice/list";
    }

    @GetMapping("/delete")
    public String deleteNotice(Long noticeId, RedirectAttributes attributes) {
        ServerResponse response = noticeService.deleteNotice(noticeId);
        if (!response.isSuccess()) {
            attributes.addFlashAttribute("errorMsg", response.getMsg());
        }
        return "redirect:/notice/list";
    }

    @GetMapping("/getFirstNotice")
    @ResponseBody
    public ServerResponse getFirstNotice(){
       return noticeService.getFirstNotice();
    }

    @GetMapping("/findNotice")
    public String getNotice(Long noticeId,Model model){
        ServerResponse<Notice> response = noticeService.findNotice(noticeId);
        Notice data = response.getData();
        model.addAttribute("notice",data);
        return "page/notice-detail";
    }
}
